package com.bdc.whatwhat.views.drawer;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.bdc.whatwhat.R;
import com.bdc.whatwhat.adapters.ObjectAdapter;
import com.bdc.whatwhat.modele.WhatWhat;
import com.bdc.whatwhat.modele.Whatter;
import com.bdc.whatwhat.session.SessionManager;
import com.bdc.whatwhat.task.AjouterAmisTask;
import com.bdc.whatwhat.task.FaireWhatWhatTask;
import com.bdc.whatwhat.task.MesAmisTask;
import com.bdc.whatwhat.task.RechercheUtilisateurTask;
import com.bdc.whatwhat.views.cell.WhatterCell;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by gfimac on 22/07/2014.
 */
public class RightDrawer extends LinearLayout implements TextWatcher, MesAmisTask.MesAmisTaskDelegate, AjouterAmisTask.AjouterAmisTaskTaskDelegate, RechercheUtilisateurTask.RechercheUtilisateurDelegate{

    ListView listViewAmis;

    List<Whatter> whattersAmis = new ArrayList<Whatter>();
    ObjectAdapter<Whatter> adapter_amis;
    String text = "";
    EditText editText;
    RechercheUtilisateurTask task;
    SessionManager session;
    int page = 1;
    int whattersAmisPage = 1;

    boolean clear = false;

    ObjectAdapter.ObjectAdapterLoadMore amisLoadMoreListener = new ObjectAdapter.ObjectAdapterLoadMore() {
        @Override
        public void loadMore() {

        }
    };

    public RightDrawer(Context context) {
        super(context);
    }

    public RightDrawer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public RightDrawer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        session = new SessionManager(getContext());

        charger();
        remplir();
        ajouterListeners();
    }

    public void chargerAmis() {
        if (whattersAmisPage == 1)
            clear = true;

        new MesAmisTask(this).execute(whattersAmisPage, session.getToken());
    }

    public void miseAJourListes() {
        whattersAmisPage = 1;
        chargerAmis();
    }

    private void charger() {
        listViewAmis = (ListView) findViewById(R.id.liste_waters);
        editText = (EditText) findViewById(R.id.recherche_whatter_edit);
    }

    private void remplir() {
        adapter_amis = new ObjectAdapter<Whatter>(getContext(), whattersAmis, R.layout.cell_whatter, WhatterCell.class,this);
        listViewAmis.setAdapter(adapter_amis);

        if(!isInEditMode()) {
            List<Whatter> mesAmis = session.getAmis();
            if (mesAmis != null) {
                ajouterListeAmis(mesAmis);
                clear = true;
            }
        }

    }

    private void ajouterListeners() {
        adapter_amis.setListener(amisLoadMoreListener);
        editText.addTextChangedListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        // On récupère le texte rentré par l'utilisateur
        text = charSequence.toString();

        // On remet la première page
        page = 1;

        if (task != null)
            task.cancel(true);

        clear = true;

        if (text.length() == 0)
            // Si il n'y a plus rien dans la recherche on remet les amis
            new MesAmisTask(this).execute(1, session.getToken());
        else
            new RechercheUtilisateurTask(this).execute(text, page, session.getToken());
    }

    @Override
    public void afterTextChanged(Editable editable) {
    }


    private void ajouterListeAmis(List<Whatter> whatters) {
        if (clear)
            whattersAmis.clear();

        whattersAmis.addAll(whatters);
        adapter_amis.notifyDataSetChanged();

        session.setAmis(whattersAmis);
    }

    //------------------------ FONCTION DELEGATE -------------------------

    @Override
    public void afficherChargement(boolean afficher) {
    }

    @Override
    public void onRechercheUtilisateurRecu(List<Whatter> whatters) {
        ajouterListeAmis(whatters);
    }

    @Override
    public void onResultatVide() {
        clear = true;
        whattersAmis.clear();
        adapter_amis.notifyDataSetChanged();
    }

    @Override
    public void onAmisAjouter() {
    }

    @Override
    public void onMesAmisRecu(List<Whatter> whatters) {
        ajouterListeAmis(whatters);
    }

    @Override
    public void onMesAmisResultatVide() {
        adapter_amis.setListener(null);
    }

    @Override
    public void onErreurReseau() {
        Toast.makeText(getContext(), getResources().getString(R.string.erreur_reseau), Toast.LENGTH_SHORT).show();
    }
}
